#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import time
import math

# 设置标准输出编码为UTF-8（Windows兼容性）
if sys.platform == "win32":
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
    sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')

def calculate_area():
    """计算各种图形面积的交互式计算器"""
    print("🔢 欢迎使用面积计算器！")
    print("=" * 40)
    
    while True:
        print("\n请选择要计算的图形:")
        print("1. 矩形")
        print("2. 圆形") 
        print("3. 三角形")
        print("4. 梯形")
        print("5. 退出程序")
    
        print("请输入选择 (1-5): ")

        try:
            choice = int(input())
            
            if choice == 1:
                calculate_rectangle()
            elif choice == 2:
                calculate_circle()
            elif choice == 3:
                calculate_triangle()
            elif choice == 4:
                calculate_trapezoid()
            elif choice == 5:
                print("感谢使用！再见！👋")
                break
            else:
                print("❌ 无效选择，请输入 1-5 之间的数字！")
                
        except ValueError:
            print("❌ 请输入有效的数字！")
        except KeyboardInterrupt:
            print("\n\n程序被用户中断")
            break

def get_positive_number(prompt):
    """获取正数输入"""
    while True:
        print(prompt)
        try:
            value = float(input())
            if value <= 0:
                print("❌ 请输入正数！")
                continue
            return value
        except ValueError:
            print("❌ 请输入有效的数字！")

def calculate_rectangle():
    """计算矩形面积"""
    print("\n📐 矩形面积计算")
    print("-" * 20)
    
    length = get_positive_number("请输入长度: ")
    width = get_positive_number("请输入宽度: ")
    
    area = length * width
    perimeter = 2 * (length + width)
    
    print(f"\n✅ 计算结果:")
    print(f"   长度: {length}")
    print(f"   宽度: {width}")
    print(f"   面积: {area:.2f} 平方单位")
    print(f"   周长: {perimeter:.2f} 单位")

def calculate_circle():
    """计算圆形面积"""
    print("\n🔵 圆形面积计算")
    print("-" * 20)
    
    radius = get_positive_number("请输入半径: ")
    
    area = math.pi * radius ** 2
    circumference = 2 * math.pi * radius
    
    print(f"\n✅ 计算结果:")
    print(f"   半径: {radius}")
    print(f"   面积: {area:.2f} 平方单位")
    print(f"   周长: {circumference:.2f} 单位")

def calculate_triangle():
    """计算三角形面积"""
    print("\n🔺 三角形面积计算")
    print("-" * 20)
    
    base = get_positive_number("请输入底边长度: ")
    height = get_positive_number("请输入高度: ")
    
    area = 0.5 * base * height
    
    print(f"\n✅ 计算结果:")
    print(f"   底边: {base}")
    print(f"   高度: {height}")
    print(f"   面积: {area:.2f} 平方单位")

def calculate_trapezoid():
    """计算梯形面积"""
    print("\n🔶 梯形面积计算")
    print("-" * 20)
    
    top_base = get_positive_number("请输入上底长度: ")
    bottom_base = get_positive_number("请输入下底长度: ")
    height = get_positive_number("请输入高度: ")
    
    area = 0.5 * (top_base + bottom_base) * height
    
    print(f"\n✅ 计算结果:")
    print(f"   上底: {top_base}")
    print(f"   下底: {bottom_base}")
    print(f"   高度: {height}")
    print(f"   面积: {area:.2f} 平方单位")

if __name__ == "__main__":
    try:
        calculate_area()
    except Exception as e:
        print(f"\n❌ 程序出现错误: {e}")
        sys.exit(1)
